package com.matrix.draft.sockettest.protocoltcp.handler;

import com.matrix.draft.sockettest.protocoltcp.pojo.ProtocolMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class ProtocolEncoder extends MessageToByteEncoder<ProtocolMessage> {
    @Override
    protected void encode(ChannelHandlerContext ctx, ProtocolMessage msg, ByteBuf out) throws Exception {
        // 将对象全量写入buffer里,前面是消息内容长度,后面是消息内容
        out.writeInt(msg.getLen())
                .writeBytes(msg.getContent());
    }
}
